﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using ThreeByte.ActiveDeck.Config;
using System.Xml.Linq;
using System.Net;
using System.Xml;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Data;
using ThreeByte.ActiveDeck.Service;
using System.Threading;
using ThreeByte.ActiveDeck.Content;
using Microsoft.Office.Interop.PowerPoint;
using ThreeByte.ActiveDeck.AddIn.Config;
using ThreeByte.Media;
using ThreeByte.ActiveDeck.AddIn.Controls;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;


namespace ThreeByte.ActiveDeck.Controls
{
    /// <summary>
    /// Interaction logic for ConditionInspectorControl.xaml
    /// </summary>
    public partial class ConditionInspectorControl : UserControl, ICloseable, INotifyPropertyChanged
    {

        public event EventHandler<DialogResultEventArgs> Close;
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName){
            if(PropertyChanged != null){
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private CoreConfig _config;

        public ConditionInspectorControl(CoreConfig config) {
            InitializeComponent();
            Tag = "Configure Layer Condition"; //Implemented to support customized title bar in Forms Host

            ConditionString.DataContext = this;
            DoneButton.DataContext = this;

            _config = config;
            List<PageConfig> possibleQuestions = new List<PageConfig>();

            foreach(int slideId in config.SlideIDs) {
                foreach(PageConfig page in config[slideId].Pages) {
                    if(page is MultipleChoiceConfig) {
                        possibleQuestions.Add(page);
                    }
                }
            }

            QuestionList.ItemsSource = possibleQuestions;
        }



        private void CancelButton_Click(object sender, RoutedEventArgs e) {
            if(Close != null) {
                Close(this, new DialogResultEventArgs(System.Windows.Forms.DialogResult.Cancel));
            }
        }

        private string _condition;
        public string Condition {
            get {
                return _condition;
            }
            private set {
                _condition = value;
                NotifyPropertyChanged("Condition");
            }
        }

        
        

        private void DoneButton_Click(object sender, RoutedEventArgs e) {
            
            if(Close != null) {
                Close(this, new DialogResultEventArgs(System.Windows.Forms.DialogResult.OK));
            }
        }

        private void ValueList_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            //Set the Condition String
            //Take the currently selected question and value
            MultipleChoiceConfig question = QuestionList.SelectedItem as MultipleChoiceConfig;
            int answerValue = ValueList.SelectedIndex;
            if(question != null && answerValue >= 0) {
                Condition = string.Format("Value({0})=={1}", question.PageId, answerValue + 1);
            } else {
                Condition = null;
            }

        }
       
    }
}
